package demo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class XMlDemo {
    public static void main(String[] args) {
        //指定需要解析的XML文件路径
        String xml="src/main/java/demo/Demo.xml";

        try{
            //创建一个SAXReader对象
            SAXReader reader=new SAXReader();

            //读取XML文件，获得Document对象
            Document document=reader.read(new File(xml));

            //获取XML文件的根节点
            Element element=document.getRootElement();

            //获取根节点下的所有子节点
            List<Element> list=element.elements("airlines");
            for (Element airlines:list){
                System.out.println("航班信息:");
                System.out.println("航空公司：" + airlines.elementText("Company"));
                System.out.println("起飞机场：" + airlines.elementText("StartDrome"));
                System.out.println("到达机场：" + airlines.elementText("ArriveDrome"));
                System.out.println("起飞时间：" + airlines.elementText("StartTime"));
                System.out.println("到达时间：" + airlines.elementText("ArriveTime"));
                System.out.println("机型：" + airlines.elementText("Mode"));
                System.out.println("是否经停：" + airlines.elementText("AirlineStop"));
            }
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }
}
